package WWW.SST.LIB;

/** 
 Interface ��ժҪ˵����
 
 
*/
////	/// �������͡�ÿ�����װ����������Ӧ����Ӧ��Ϣ�Ľ�������Ϊ��������Ϣ�Ľ�������+400������ǰ֧�����½������ͣ�
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#region
//	��������
//	˵��
//	��������
//	˵��
//	101
//	����֪ͨ����
//	501
//	����֪ͨ��Ӧ
//	102
//	���ڲ�ѯ����
//	502
//	���ڲ�ѯ��Ӧ
//	103
//	Ͷע����
//	503
//	Ͷע��Ӧ
//	104
//	Ͷע���֪ͨ����
//	504
//	Ͷע���֪ͨ��Ӧ
//	105
//	Ʊ��ѯ����
//	505
//	Ʊ��ѯ��Ӧ
//	106
//	������ѯ����
//	506
//	������ѯ��Ӧ
//	107
//	������ѯ����
//	507
//	������ѯ��Ӧ
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#endregion
public class Interface extends MOLE.App.Every
{

	private String Span = "";
	private boolean IsOk = false;
public static int AgentId = MOLE.GetValue.GetConfigInt("JK_AgentId");
public String AgengPawwword = MOLE.GetValue.GetConfigString("JK_AgentPwd");
public String SendUrl = MOLE.GetValue.GetConfigString("JK_SendURL");
public String ReturnPost = "";

	private String MsgOriTxt = "";
	public String Msg = "";
	private String head = "";
	private String body = "";
	public int Type = 0;
	public String Paras = "";

	public int StausCode = 1;
	public String StausMsg = "Err";

	public System.Xml.XmlDocument doc;


	public String TimeStamps = "0";
	public String TimeStamp = "0"; //yyyyMMddHHmmss
	public String MessageId = "0"; //22veir

	public Interface()
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
		TimeStamps = MOLE.Time.getTimeStamp(16);
		TimeStamp = TimeStamps.substring(0,14); //yyyyMMddHHmmss
		MessageId = String.valueOf(AgentId) + TimeStamps; //22veir


		String Isokstr = MOLE.GetValue.GetConfigString("JK_Statu").toLowerCase().trim();
		if (Isokstr.equals("true"))
		{
			IsOk = true;
		}
		else
		{
			IsOk = false;
		}
	}
	//string Result="";



	//		// Response.Write(strRet); 
	public final String getMsg()
	{
		String p = "transType=" + String.valueOf(this.Type) + "&transMessage=";
		String para = "<message version=\"1.0\" id=\"" + MessageId + """>" + this.head + (this.body) + "</message>";
		this.MsgOriTxt = p + para;
		String s = p + System.Web.HttpUtility.UrlEncode(para);
		return s;
	}

	public final String getMsgOnly()
	{

		String s = "<message version=\"1.0\" id=\"" + MessageId + """>" + this.head + this.body + "</message>";
		this.MsgOriTxt = s;

		return s;
	}

	public final String getHead()
	{
		String signstr = ((String)(MessageId + TimeStamp + AgengPawwword + this.body)).replace("\r","").replace("\n","");
		String signlen = signstr.length().toString();
		String head = Span + "<header>" + "<messengerID>" + String.valueOf(AgentId) + "</messengerID>" + " <timestamp>" + TimeStamp + "</timestamp>" + "<transactionType>" + String.valueOf(this.Type) + "</transactionType>" + "<digest>" + MOLE.EncryptDecrypt.EncryptMD5(signstr).toLowerCase() + "</digest>" + "</header>";
		return head;
	}


	/** 
	 Pyao tsaSywen  105
	 
	 @param ticketIds
	 @return 
	*/
	public final String GetBody_105(String ticketIds)
	{
		/*
		<body>
		<ticketQuery>
		<ticket id="3899122007051000000231"/>
		<ticket id="3899122007051000000232"/>
		</ticketQuery>
		</body>
		*/

		String[] s = ticketIds.split("[,]", -1);
		String ticked = "";
		for (int i = 0;i < s.length;i++)
		{
			MOLE.Bill.Bill o = new MOLE.Bill.Bill(Integer.parseInt(s[i]));
			ticked += Span;
			ticked += "<ticket" + " id=" + "\"" + this.getTicketId(o.BillID) + "\"" + " />";
		}
		String body = Span + "<body><ticketQuery>" + ticked + "</ticketQuery></body>";
		//body=System.Web.HttpUtility.UrlEncode( body );
		return body;

	}
	public final String getTicket(String ticketIds)
	{
		String[] s = ticketIds.split("[,]", -1);
		String ticked = "";
		for (int i = 0;i < s.length;i++)
		{
			MOLE.Bill.Bill o = new MOLE.Bill.Bill(Integer.parseInt(s[i]));
			ticked += Span;
			ticked += "<ticket" + " id=\"" + this.getTicketId(o.BillID) + "\"" + " playType=\"" + getPlayType(o.Playid) + "\"" + " money=\"" + String.valueOf(o.BillValue) + "\"" + " amount=" + """" + String.valueOf(o.Zhushor) + """" + ">" + "<issue number=\"" + o.Term.replace("-","") + """ gameName=\"ssc\" />" + "<userProfile cardType=\"1\" mail=\"wangye@sun.com\"" + " cardNumber=\"123456789123456789\"" + " mobile=\"1111111111\"" + " realName=\"none\" bonusPhone=\"1111111\" />" + "<anteCode>" + _ConvertNumberFormat(o.Playid, o.Number) + "</anteCode>" + "</ticket>";
				//TERM INFO
				//USER INFO
				//NUMBER

			//					WWW.SST.LIB.TicketPost oo=new TicketPost();
			//					oo.TicketId=o.BillID;
			//					oo.TicketIdX=this.getTicketId( o.BillID );
			//					oo.Add();
		}
		String body = Span + "<body><lotteryRequest>" + ticked + "</lotteryRequest></body>";
		//body=System.Web.HttpUtility.UrlEncode( body );
		return body;
	}


			public final String _GetTicketBody(String ticketIds)
			{
	////			<body>
	////<ticketNotify>
	////<ticket id="3899122007051000000231"
	////dealTime="20070510165423"
	////status="0000" message=""/>
	////<ticket id="3899122007051000000232"
	////dealTime="20070510165424"
	////status="2012" message="��ֹ��Ͷ"/>
	////</ticketNotify>
	////</body>
//					string[] s=ticketIds.Split(',');
//					string ticked="";
//					for(int i=0;i<s.Length;i++)
//					{
//						
//						ticked+=Span;
//						ticked+=@"<ticket"
//							+" id=\""+  MOLE.comm.AddPreZero( s[i] ,22 ).Trim()+"\""
//							+" dealTime=\""  + TimeStamp + "\""
//							+ " status=\""+MOLE.comm.AddPreZero(this.StausCode,4).Trim() + "\""
//							+ " message=\""+ this.StausMsg +"\"" 
//						 	+" />";
//							 
//					}
//					string body=Span+"<body><ticketNotify>" + ticked +"</ticketNotify></body>";
				//body=System.Web.HttpUtility.UrlEncode( body );
		 return body;
			}



	public final String getTicketId(int tickIdstr)
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		String tickId = String.valueOf(tickIdstr);
		String s = "";
		for (int i = 0;i < (22 - tickId.length());i++)
		{
			s += "0";
		}
		s = s + tickId;

		String r = TimeStamp + s;
		r = s;
		return r;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}


	public final String getPlayType(int playid)
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		String s = MOLE.Bill.BillResx.BillType;
		String[] ss = s.split("[,]", -1);
		String r = "";
		for (int i = 0;i < ss.length;i++)
		{
			if (ss[i].trim().equals(String.valueOf(playid)))
			{
				return ss[i + 1];
			}
		}
		return "";
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}


	public final void getRespose(String str)
	{
		String strx = str;
		str = System.Web.HttpUtility.UrlDecode(str);
		str = str.substring(27, str.length());
		doc = MOLE.XML.getXML(str);

		String s_mid = doc.SelectSingleNode("message").Attributes["id"].Value.toString();
		String s_time = doc.SelectSingleNode("message/header/timestamp").InnerXml.toString();
		String s_digest = doc.SelectSingleNode("message/header/digest").InnerXml.toString().trim();
		String s_body1 = "<body>" + doc.SelectSingleNode("message/body").InnerXml.toString() + "</body>";
		String s_body = str.substring(str.indexOf("<body>"), str.length() - 10);
		String s_jiami = s_mid + s_time + AgengPawwword + s_body;
		s_jiami = MOLE.EncryptDecrypt.MD5(s_mid + s_time + AgengPawwword + s_body, "GB2312").toLowerCase();
		if (s_digest.equals(s_jiami))
		{
			XmlNode zi = doc.SelectSingleNode("message/body/response");
			this.StausCode = Integer.parseInt(zi.Attributes["code"].Value.toString());
			this.StausMsg = zi.Attributes["message"].Value.toString();
		}
		else
		{
			this.StausCode = 1;
			this.StausMsg = "��ϢժҪ��ƥ��";
		}

	}


	public final String getBodyResponse()
	{
		String sendbody = "<body><response code=\"" + this.StausCode + "\" message=\"" + this.StausMsg + "\" /></body>";
		this.body = sendbody;
		return sendbody;

	}

	public final String tidSearch(String s_tid)
	{
		String sendbody = "<body><ticketQuery><ticket id=\"" + s_tid + "\"/></ticketQuery></body>";
		this.body = sendbody;
		return sendbody;
	}
	public final String getSearch(String strQiHao)
	{
		String s_str = "<body>";
		s_str += "<bonusQuery>";
		s_str += "<issue gameName=\"ssc\" number=\"" + strQiHao + """ />";
		s_str += "</bonusQuery>";
		s_str += "</body>";
		return s_str;
	}


	public final String getSale(String strQiHao)
	{
		String s_str = "<body>";
		s_str += "<balanceQuery>";
		s_str += "<issue gameName=\"ssc\" number=\"" + strQiHao + """ />";
		s_str += "</balanceQuery>";
		s_str += "</body>";
		return s_str;
	}

	public final void Send()
	{
		if (!IsOk)
		{
			return;
		}

		System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
		if (this.Type == 103)
		{
			Bet();
		}
		else if (this.Type == 104)
		{
			�� _BetNotify();
		}
		else if (this.Type == 504)
		{
			BetNotifyRespone();
		}
		else if (this.Type == 105)
		{
			teckitSearch();
		}
		else if (this.Type == 106)
		{
			BackSearch();
		}
		else if (this.Type == 107)
		{
			BackSale();
		}
//			else if(this.Type==1055) //my pyao tsasyon
//			{
//				
//			}






		//sign 
		//get hean
		//sign body 
		//head+body 
		//send
	}

	public final void Bet()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		this.body = getTicket(this.Paras);
		//this.body="<body><lotteryRequest><ticket id=\"200999200707250917580111\" playType=\"201\" money=\"32.0\" amount=\"4\"><issue number=\"2007072501\" gameName=\"ssc\"/><userProfile cardType=\"0\" mail=\"wangye@sun.com\" cardNumber=\"430525800815722\" mobile=\"13612736754\" realName=\"����֮\" bonusPhone=\"(0755)65079323\"/><anteCode>3,1,9</anteCode><anteCode>1,1,0</anteCode><anteCode>6,7,6</anteCode><anteCode>9,0,7</anteCode></ticket><ticket id=\"200999200707250917580014\" playType=\"201\" money=\"2.0\" amount=\"1\"><issue number=\"2007072501\" gameName=\"ssc\"/><userProfile cardType=\"0\" mail=\"wangye@sun.com\" cardNumber=\"430525800815722\" mobile=\"13612736754\" realName=\"����֮\" bonusPhone=\"(0755)65079323\"/><anteCode>4,2,3</anteCode></ticket></lotteryRequest></body>";
		this.head = getHead();
		this.Msg = getMsg();
		String strRet = "";


		//save
		WWW.SST.LIB.TicketPost.AddArr(Paras, TimeStamp);

		try //sent
		{

			//string BetReceiveUrl=MOLE.GetValue.GetConfigString("BetReceiveUrl");
			String BetReceiveUrl = SendUrl;
			strRet = MOLE.xmlHttp.Post(BetReceiveUrl, this.Msg);
			//set status	
			this.getRespose(strRet);
		}
		catch (RuntimeException ee)
		{
			this.StausCode = 1;
			this.StausMsg = ee.toString();
			strRet = ee.toString();
			MOLE.App.Log.AddDB("this.getRespose err", ee.toString());

		}

		//set result 
		WWW.SST.LIB.TicketPost.SetResults(this.Paras, this.StausCode);

		//log
		log(this.Msg, strRet);
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

	public final void _BetResponse()
	{

	}

	public final void _BetNotify()
	{
		this.body = _GetTicketBody(this.Paras);
		this.head = getHead();
		this.Msg = getMsg();
		String strRet = "";


		//sent
		strRet = MOLE.xmlHttp.Post("http://localhost:777/5186/post4.aspx", this.Msg);
		 this.getRespose(strRet);



//			//set result 
//			WWW.SST.LIB.TicketPost.SetResults(this.Paras,this.StausCode );
//			//log
//			log(strRet);

	}
	public final void BetNotifyRespone()

	{
		this.body = "<body><response code=\"0000\" message=\"�ɹ�,ϵͳ��������\"/></body>";
		this.head = getHead();
		this.Msg = getMsg();
		System.Web.HttpContext.Current.Response.Write(this.Msg);

	}

	public static void log(String send, String receive)
	{
		String msg = System.Web.HttpUtility.UrlDecode(send);
		MOLE.SQLOS o = new MOLE.SQLOS("daigoupostlog");
		o.RS("send", System.Web.HttpUtility.HtmlEncode(msg));
		o.RS("result", System.Web.HttpUtility.HtmlEncode(receive));
	//	dpProcessNotifyTimes
		o.Insert();
	}


	public final void teckitSearch()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		this.body = tidSearch(this.Paras);
		this.head = getHead();
		this.Msg = getMsg();
		String strRet = "";
		//save
		try
		{
			//sent
			//string BetReceiveUrl=MOLE.GetValue.GetConfigString("http://localhost:777/5186/search_back.aspx");
			strRet = MOLE.xmlHttp.Post(SendUrl, this.Msg);
			this.getRespose(strRet);
			log(this.Msg, strRet);
		}
		catch (RuntimeException ee)
		{
			this.StausCode = 1;
			this.StausMsg = ee.toString();
			strRet = ee.toString();
			MOLE.App.Log.AddDB("this.getRespose err", ee.toString());

		}
		finally
		{
			//log
			ReturnPost = strRet;
			log(this.Msg, strRet);
		}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}
	public final void BackSearch()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		this.body = getSearch(this.Paras);
		this.head = getHead();
		this.Msg = getMsg();
		String strRet = "";
		//save
		try
		{
			//sent
			//string BetReceiveUrl=MOLE.GetValue.GetConfigString("http://localhost:777/5186/search_back.aspx");
			strRet = MOLE.xmlHttp.Post(SendUrl, this.Msg);
			this.getRespose(strRet);
			log(this.Msg, strRet);
		}
		catch (RuntimeException ee)
		{
			this.StausCode = 1;
			this.StausMsg = ee.toString();
			strRet = ee.toString();
			MOLE.App.Log.AddDB("this.getRespose err", ee.toString());

		}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}
	public final void BackSale()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		this.body = getSale(this.Paras);
		this.head = getHead();
		this.Msg = getMsg();
		String strRet = "";
		//save
		try
		{
			//sent
			//string BetReceiveUrl=MOLE.GetValue.GetConfigString("http://localhost:777/5186/search_back.aspx");
			strRet = MOLE.xmlHttp.Post(SendUrl, this.Msg);
			this.getRespose(strRet);
			log(this.Msg, strRet);
		}
		catch (RuntimeException ee)
		{
			this.StausCode = 1;
			this.StausMsg = ee.toString();
			strRet = ee.toString();
			MOLE.App.Log.AddDB("this.getRespose err", ee.toString());

		}
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

	public final String _ConvertNumberFormat(int playid, String number)
	{
//				public static string  BillType="��ѡ����,50120001,301,��ѡ����,50120002,301,��ѡ����,50120003,301,��ѡһ��,50120004,301"
//			+",�������,50120011,303,�������,50120012,303,��϶���,50120013,303,���һ��,50120014,303"
//			+",��ֵ����,50120005,304,��ֵ����,50120006,305,"
//			+"��С��˫,50120040,306"
//			+"������ѡ��ѡ,50120050,307,������ѡ��ʽ,50120051,308,������ѡ��λ��ѡ,50120052,309,������ѡ����,50120053,301,������ѡ����,50120054,311";
		String s = "";
		if (playid == 50120040) //dsds
		{
			//1234>>2154
			//12>> 2,1
			s = number.replace("1","da").replace("2","sy").replace("3","de").replace("4","sh");
			s = s.replace("da","2").replace("sy","1").replace("de","5").replace("sh","4");
			s = s.substring(0,1) + "," + s.substring(1, 2);
		}
		else if (playid == 50120050) //������ѡ��ѡ
		{
			s = "_,_,_," + number.substring(0,1) + "," + number.substring(1, 2);
		}
		else if (playid == 50120051)
		{
			s = MOLE.str.InsertSpan(number, ",");
		}
		else if (playid == 50120052) //fenveir
		{
			s = "_,_,_," + number;
		}
		else if (playid == 50120053) //�������
		{
			s = number;
		}
		else if (playid == 50120054) //baodae
		{
			//s= "_,_,_,"+number;
			s = number;
		}



		else //stard  and hezhi
		{
			s = number.replace("-","_").replace("|",",");
		}

		 return s;

	}



	public static String GetBetProcessStatu(String Tids)
	{
		String tid = Tids;
		String r = "";


		WWW.SST.LIB.Interface o = new WWW.SST.LIB.Interface();
		//onley host test
		WWW.SST.LIB.Interface.AgentId = 900003;
		o.AgengPawwword = "cqssc8";
		o.SendUrl = "http://222.177.14.198:8080/common/HttpInterFaceServlet";
		//


		String s_tid = tid;
		s_tid = MOLE.comm.AddPreZero(s_tid, 22);



		o.Type = 105;
		o.Paras = s_tid;
		o.Send();
		//labRetPost.Text=o.ReturnPost;
		if (o.StausCode == 0)
		{
			System.Xml.XmlDocument doc = o.doc;
			XmlNode node = doc.SelectSingleNode("//body/response/ticketQueryResult/ticket");
			String str_html = "";
			//	str_html+="<div>Ʊ�ţ�"+node.Attributes["id"].Value.ToString()+"</div>";					
			//	str_html+="<div>���������"+node.Attributes["message"].Value.ToString()+"</div>";	
			String status = node.Attributes["status"].Value.toString();
			if (status.equals("0000") || status.equals("2048"))
			{
				r = "cp";
			}
			else if (status.equals("2049"))
			{

				r = "fq";
			}
			else
			{
				r = "fq";
			}

		}
		else
		{
			r = "un";
		}
	//	return  tid+","+r;
		return r;
	}

}